{

capture drop sample
gen sample=(S_pds==1 & n_ud==1) 	
capture drop b_in_sample
bys b_id: egen b_in_sample=max(sample)

capture drop sbjt_in_sample
capture drop sample
gen sample=(S_pds==1 & n_ud==1) 	
bys gr_sbjt: egen sbjt_in_sample=max(sample)


* --> Market share

sort $isid_vars 
capture drop xx
gen xx=q_exp if S_p==1
replace xx=0 if S_p==0 		
bys b_id: egen q_b_p=total(xx)
capture drop QQQ
egen QQQ=total(xx)

gen sh_b_p=q_b_p/QQQ


* --> Ranking in size

capture drop xx
gen xx=sh_b_p if n_b==1 & sh_b_p!=. & sh_b_p!=0
capture drop yy
egen yy=rank(-xx), unique
bys b_id: egen rank_b_p_size=mean(yy) 

sum rank_b_size if n_b==1 & b_in_sample==1, d

capture drop xx
gen xx=ln_q_b_all if n_b==1 & ln_q_b_all!=. & b_in_sample==1
capture drop yy
egen yy=rank(-xx), unique
bys b_id: egen Z=mean(yy) 	
list Z b_id if n_b==1 & Z<=10

gen rank_floor=floor(Z/25)

* --> Sellers per year (in relevant products)

capture drop xx
gen xx=s_id if  S_p==1
bys gr_bt: egen num_s_in_p_bt=nvals(xx)
capture drop yy
bys gr_bt: egen yy=median(num_s_in_p_bt)
replace num_s_in_p_bt=yy
replace num_s_in_p_bt=0 if num_s_in_p_bt==.

capture drop xx 
gen xx=num_s_in_p_bt if n_bt==1
bys b_id: egen av_num_s_in_p_b=mean(xx)

* --> Ranking in relational metric

capture drop rank_b_rel
capture drop rank_b_rel_top

capture drop xx
gen xx=incl_sourcing_metric_b if n_b==1 & b_in_sample==1

capture drop yy
egen yy=rank(-xx), unique
bys b_id: egen rank_b_rel=mean(yy) 

capture drop xx
gen xx=incl_sourcing_metric_b if n_b==1 & b_in_sample==1 & rank_b_size<=25  

capture drop yy
egen yy=rank(-xx), unique
bys b_id: egen rank_b_rel_top=mean(yy) 

list rank_b_size b_id rank_b_rel_top if n_b==1 & rank_b_size<=10 & b_in_sample==1

* --> Price ranking

reghdfe ln_p_sbjo ln_q_sbjo if n_ud==1 & S_pds==1, abs(b_id gr_sjt, savefe)
capture drop zz
bys b_id: egen zz=mean(__hdfe1__)
capture drop xx
gen xx=zz if n_b==1 & b_in_sample==1 & rank_b_size<=25 
capture drop yy
egen yy=rank(-xx), unique
bys b_id: egen rank_b_p_top=mean(yy) 


list rank_b_size b_id sh_b_p av_num_s_in_p_b rank_b_rel_top rank_b_p_top  if n_b==1 & rank_b_size<=25 & b_in_sample==1, noobs
tabstat sh_b_p if n_b==1 & b_in_sample==1, stat(mean median sd iqr N)
tabstat num_s_in_p_bt if n_b==1 & b_in_sample==1, stat(mean median sd iqr N)


* Top 100
capture drop xx
gen xx=sh_b_p if Z<=100 & n_b==1
egen sh_p_top100=total(xx)
sum sh_p_top100

tabstat xx if n_b==1 & b_in_sample==1 & Z<=100, stat(mean median sd )
tabstat num_s_in_p_bt if n_b==1 & b_in_sample==1 & Z<=100, stat(mean median sd )

tabstat xx if n_b==1 & b_in_sample==1, stat(mean median sd iqr N)

eststo O_T1_B: estpost tabstat xx num_s_in_p_bt if n_b==1 & b_in_sample==1 & Z<=100, statistics(mean p50 sd ) columns(statistics)	
	esttab O_T1_B using "${tables}\Table1PanelB.tex", cells("Mean(label(Mean) fmt(%9.2fc)) p50(label(P50) fmt(%9.2fc)) SD(label(Std. Deviation) fmt(%9.2fc))") nomtitles label nonumbers noobs unstack replace posthead("`detail'" "`numbers'" "`subhead'") nodep collabels(none) prehead(" ") postfoot(" ")
	
eststo O_T1_C: estpost tabstat sh_b_p num_s_in_p_bt if n_b==1 & b_in_sample==1, statistics(mean p50 sd ) columns(statistics)	
	esttab O_T1_C using "${tables}\Table1PanelC.tex", cells("Mean(label(Mean) fmt(%9.2fc)) p50(label(P50) fmt(%9.2fc)) SD(label(Std. Deviation) fmt(%9.2fc))") nomtitles label nonumbers noobs unstack replace posthead("`detail'" "`numbers'" "`subhead'") nodep collabels(none) prehead(" ") postfoot(" ")
	
	
* Top 25 Buyers
capture drop xx
gen xx=sh_b_p*100
sort rank_b_size

list rank_b_size b_id xx av_num_s_in_p_b rank_b_rel_top rank_b_p_top  if n_b==1 & rank_b_size<=25 & b_in_sample==1, noobs

preserve
keep rank_b_size b_id sh_b_p av_num_s_in_p_b rank_b_rel_top rank_b_p_top n_b b_in_sample
keep if n_b==1 & rank_b_size<=25 & b_in_sample==1
listtex * using "${tables}\Table1PanelA.tex", replace
restore


} 
